package com.mk.service.platform;

import act.controller.Controller;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.Api;
import com.github.d925529.apidoc.annotation.ApiMethod;
import com.github.d925529.apidoc.annotation.ApiParam;
import com.github.d925529.apidoc.annotation.ApiReturn;
import com.mk.domain.TProduct;
import com.mk.domain.TProductSpec;
import com.mk.enums.GlobalValues;
import com.mk.interceptor.ITranslate;
import com.mk.util.S2;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.S;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 说明：商品表后台服务
 * 作者：292303959@qq.com 
 * 日期：2018-10-17 10:5:20 
 * 版本：1.0.0
 */
@Controller("tProduct")
@Api(group = "pl",title = "商品表服务", path = "/tProduct")
@SuppressWarnings("unused")
public class TProductService extends BasePlatformService {
    @Inject
    private TProduct.Mapper tProductMapper;
    @Inject
    private TProductSpec.Mapper tProductSpecMapper;


    @With({ILogin.class, ITranslate.class})
    @PostAction("add.json")
    @ApiMethod(title = "增加保存商品表", path = "/add.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "tProduct", title = "商品表", type = TProduct.class)
    @ApiReturn(description = "主键", type = String.class)
    public String add(@NotNull TProduct tProduct,String skuList) {
        String id = S2.UUID();
        tProduct.setPdid(id);
        tProduct.setPdstate(GlobalValues.Z_PD_STATE_0.value());
        tProduct.setPdbuilddate(DateUtil.now());
        tProductMapper.insertTemplate(tProduct);
        List<TProductSpec> sList = JSON.parseArray(skuList,TProductSpec.class);
        sList.forEach(tps->{
            tps.setPsid(S2.UUID());
            tps.setPdid(id);
            tps.setPdunit(tProduct.getPdunit());
            tProductSpecMapper.insertTemplate(tps);
        });
        return id;
    }


    @With({ILogin.class, ITranslate.class})
    @PutAction("update.json")
    @ApiMethod(title = "修改保存商品表", path = "/update.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "tProduct", title = "商品表", type = TProduct.class)
    @ApiReturn(description = "修改了的数量", type = int.class)
    public int update(@NotNull TProduct tProduct,String skuList) {
        List<TProductSpec> sList = JSON.parseArray(skuList,TProductSpec.class);
        sList.forEach(tps->{
            if(S.isNotEmpty(tps.getPsid())){
                tps.setPdunit(tProduct.getPdunit());
                tProductSpecMapper.updateTemplateById(tps);
            }else{
                tps.setPsid(S2.UUID());
                tps.setPdid(tProduct.getPdid());
                tps.setPdunit(tProduct.getPdunit());
                tProductSpecMapper.insertTemplate(tps);
            }

        });
        return tProductMapper.updateTemplateById(tProduct);
    }

    @With(ILogin.class)
    @GetAction("view.json")
    @ApiMethod(title = "查询指定商品表", path = "/view.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "id", title = "商品表的主键", type = String.class)
    @ApiReturn(description = "商品表", type = TProduct.class)
    public TProduct view(@NotNull String id){
        return tProductMapper.single(id);
    }

    @With(ILogin.class)
    @PostAction("getProduct.json")
    @ApiMethod(title = "查询指定商品表", path = "/getProduct.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "id", title = "商品表的主键", type = String.class)
    @ApiReturn(description = "商品表", type = TProduct.class)
    public TProduct getProduct(@NotNull String pdid){
        TProduct tproduct = tProductMapper.single(pdid);
        TProductSpec tProductSpec = new TProductSpec();
        tProductSpec.setPdid(pdid);
        List<TProductSpec> skuList = tProductSpecMapper.template(tProductSpec);
        tproduct.setSkuList(skuList);
        return tproduct;
    }

    @With(ILogin.class)
    @PutAction("onShell.json")
    @ApiMethod(title = "上架商品", path = "/onShell.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "id", title = "商品表的主键", type = String.class)
    @ApiReturn(description = "商品表", type = TProduct.class)
    public int onShell(@NotNull String pdid){
        TProduct tproduct = new TProduct();
        tproduct.setPdid(pdid);
        tproduct.setPdstate(GlobalValues.Z_PD_STATE_1.value());
        return tProductMapper.updateTemplateById(tproduct);
    }

    @With(ILogin.class)
    @PutAction("outShell.json")
    @ApiMethod(title = "下架商品", path = "/outShell.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "id", title = "商品表的主键", type = String.class)
    @ApiReturn(description = "商品表", type = TProduct.class)
    public int outShell(@NotNull String pdid){
        TProduct tproduct = new TProduct();
        tproduct.setPdid(pdid);
        tproduct.setPdstate(GlobalValues.Z_PD_STATE_4.value());
        return tProductMapper.updateTemplateById(tproduct);
    }

    @With(ILogin.class)
    @PutAction("auditPd.json")
    @ApiMethod(title = "审核通过商品", path = "/auditPd.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "id", title = "商品表的主键", type = String.class)
    @ApiReturn(description = "商品表", type = TProduct.class)
    public int auditPd(@NotNull String pdid){
        TProduct tproduct = new TProduct();
        tproduct.setPdid(pdid);
        tproduct.setPdshedata(DateUtil.now());
        tproduct.setPdstate(GlobalValues.Z_PD_STATE_3.value());
        return tProductMapper.updateTemplateById(tproduct);
    }
    @With(ILogin.class)
    @PutAction("rejectPd.json")
    @ApiMethod(title = "审核驳回商品", path = "/rejectPd.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "id", title = "商品表的主键", type = String.class)
    @ApiReturn(description = "商品表", type = TProduct.class)
    public int rejectPd(@NotNull String pdid){
        TProduct tproduct = new TProduct();
        tproduct.setPdid(pdid);
        tproduct.setPdstate(GlobalValues.Z_PD_STATE_0.value());
        return tProductMapper.updateTemplateById(tproduct);
    }
}
